package cn.wb.org.controller;

import cn.wb.base.util.AjaxResult;
import cn.wb.base.util.ExcelUtil;
import cn.wb.base.util.PageList;
import cn.wb.org.domain.Shop;
import cn.wb.org.dto.ShopAuditDto;
import cn.wb.org.dto.ShopDto;
import cn.wb.org.query.ShopQuery;
import cn.wb.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     *
     * @param shop 传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop) {
        try {
            if (shop.getId() != null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            shopService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id") Long id) {
        return shopService.findById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<Shop> list() {

        return shopService.findAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query) {
        return shopService.queryByPage(query);
    }


    /**
     * 店铺入住
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody @Valid ShopDto dto) {
        shopService.settlement(dto);
        return AjaxResult.me().success();
    }

    /**
     * 审核通过
     */
    @PostMapping("/audit/pass")
    public AjaxResult auditPass(@RequestBody @Valid ShopAuditDto dto) {
        shopService.auditPass(dto);
        return AjaxResult.me().success();
    }

    /**
     * 激活店铺
     */
    @GetMapping("/activity/{id}")
    public AjaxResult activity(@PathVariable("id") Long id) {
        shopService.activity(id);
        return AjaxResult.me().success();
    }

    /**
     * 导出 店铺列表数据
     */
    @GetMapping("/exprotExcel")
    public void exprotExcel(HttpServletResponse res){
        List<Shop> all = shopService.findAll();
        // 调用工具类
        ExcelUtil.exportExcel(all,"","店铺信息",Shop.class,"店铺信息.xls",
                res);
    }

    /**
     * 导入店铺列表
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        // 调工具类 导入
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 2, Shop.class);
        // 你们自己去插入数据库
        shops.forEach(System.out::println);
    }

}
